﻿/**
 * Created by pc on 2017/7/3.
 */
/**
 * Created by pc on 2017/7/3.
 */

myApp.controller("fyTree",function($scope,$state,$http,$q,$filter,$interval,$timeout,httpService,$rootScope){
	$scope.selectList_1 = [];
	$scope.selectList_2 = [];
	$scope.selectList_3 = [];
	$scope.firstIndex=0;
	$scope.secondIndex=1;
	$scope.thirdIndex=0;
	/*$scope.titleName=[$scope.selectList_1[$scope.firstIndex].name,$scope.selectList_1[$scope.firstIndex].subMenus[$scope.secondIndex].name,$scope.selectList_1[$scope.firstIndex].subMenus[$scope.secondIndex].subMenus[$scope.thirdIndex].name];
	$rootScope.$broadcast('changeTitle',{data:$scope.titleName});*/
	
	$scope.selectTree = function(item,treeIdx,index,num){
		switch (treeIdx){
			case 'selectList_2':
				$scope.selectList_2 = [];
				$scope.selectList_3 = [];
				if($scope.titleName==null){
					$scope.titleName=[item.name];
					$rootScope.$broadcast('changeTitle',{data:$scope.titleName});
				}
				break;
			case 'selectList_3':
				$scope.selectList_3 = [];
				break;
		}
		if(!treeIdx){
			if(num==1){
				$scope.titleName=[item.name];
				$rootScope.$broadcast('changeTitle',{data:$scope.titleName});
			}
			if(num==2){
				angular.forEach($scope.selectList_1,function (item01,index01) {
					angular.forEach(item01.subMenus,function (item02,index02) {
						if(item==item02){
							$scope.titleName=[$scope.selectList_1[index01].name,item.name];
							$rootScope.$broadcast('changeTitle',{data:$scope.titleName});
						}
					})
				})
				$scope.secondIndex=index;
				$scope.thirdIndex=-1;
			}
			else {
				angular.forEach($scope.selectList_1,function (item01,index01) {
					angular.forEach(item01.subMenus,function (item02,index02) {
						angular.forEach(item02.subMenus,function (item03,index03) {
							if(item==item03){
								$scope.titleName=[$scope.selectList_1[index01].name,$scope.selectList_1[index01].subMenus[index02].name,item.name];
								$rootScope.$broadcast('changeTitle',{data:$scope.titleName});
							}
						})
					})
				})
				$scope.secondIndex=-1;
				$scope.thirdIndex=index;
			}
			$state.go(item.url);
			console.log("没有下级")
		}else{
			if(item.checked){
				item.checked = false;
			}else{
				item.checked = true;
				item.subMenus.length>0 ? $scope[treeIdx] = item.subMenus : '';
			}
		}
	}
	
});

myApp.directive('fyTree',function(httpService){
	var obj = {
		restrict : "EA",
		priority : 0 ,
		terminal: false,
		templateUrl :'./model/left.html',
		replace : false,
		scope : {
			dataUrl:'@',
		},
		//可以是字符串或者函数 (公共逻辑)
		//字符串可以指定控制器名称
		//函数为自己设定控制器逻辑
		controller : "fyTree",
		link:function (scope,Element,Attrs) {
			httpService.getRequest(Attrs.url).then(function(result) {
				scope.selectList_1  =result.data[0].subMenus
			});
		}
	};
	return obj;
});